package com.rongshu.view.home.fragment.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.LoginUtils;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.home.widget.FontSizeView;
import com.utils.SPUtils;
import com.utils.constants.SPKey;

public class MultifunctionDialog extends Dialog {

    public MultifunctionDialog(@NonNull Context context) {
        super(context);
    }

    public MultifunctionDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected MultifunctionDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder {

        private Context context = null;

        private int mMainLayout = R.layout.dialog_multfunction_view;

        private FontSizeView fontSizeView;

        private LinearLayout layout_collect, layout_report;

        private ImageView iv_collect;

        private TextView txt_cancle;

        private boolean isCollect;

        private FontSizeView.OnChangeCallbackListener listener = null;

        private OnCollectReportClickListener mOnCollectReportClickListener = null;

        public Builder(Context context) {
            this.context = context;
        }

        public MultifunctionDialog.Builder setIsCollect(boolean isCollect) {
            this.isCollect = isCollect;
            return this;
        }

        public MultifunctionDialog.Builder setOnChangeCallbackListener(FontSizeView.OnChangeCallbackListener listener) {
            this.listener = listener;
            return this;
        }

        public MultifunctionDialog.Builder setOnCollectReportClickListener(OnCollectReportClickListener listener) {
            this.mOnCollectReportClickListener = listener;
            return this;
        }

        public FontSizeDialog create() {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final FontSizeDialog dialog = new FontSizeDialog(context, R.style.CustomDialogStyle);
            View layout = inflater.inflate(mMainLayout, null);
            dialog.addContentView(layout, new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            dialog.setContentView(layout);
            Window window = dialog.getWindow();
            window.setGravity(Gravity.BOTTOM);
            window.getDecorView().setPadding(0, 0, 0, 0);
            window.setWindowAnimations(R.style.DefaultAnimation);
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setAttributes(lp);
            //fontSizeView
            fontSizeView = layout.findViewById(R.id.fontSizeView);
            int fontType = SPUtils.getPreferences(SPKey.FONTPATTERN, 1);
            fontSizeView.setDefaultPosition(fontType);
            fontSizeView.setChangeCallbackListener(listener);
            //收藏，举报
            layout_collect = layout.findViewById(R.id.layout_collect);
            layout_report = layout.findViewById(R.id.layout_report);
            txt_cancle = layout.findViewById(R.id.txt_cancle);
            iv_collect = layout.findViewById(R.id.iv_collect);
            iv_collect.setImageResource(isCollect ? R.drawable.yello_catalog : R.drawable.gray_uncatalog);
            layout_collect.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (UserInfoUtils.getUserId() == 0) {
                        LoginUtils.toLogin(context);
                        return;
                    }
                    isCollect = !isCollect;
                    iv_collect.setImageResource(isCollect ? R.drawable.yello_catalog : R.drawable.gray_uncatalog);
                    if (mOnCollectReportClickListener != null) {
                        mOnCollectReportClickListener.onCollectClick();
                    }
                }
            });
            layout_report.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnCollectReportClickListener != null) {
                        mOnCollectReportClickListener.onReportClick(dialog);
                    }
                }
            });
            txt_cancle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            return dialog;
        }

        public interface OnCollectReportClickListener {

            void onCollectClick();

            void onReportClick(Dialog dialog);
        }
    }
}
